﻿#region Copyright

// // The contents of this file are subject to the Mozilla Public License
// // Version 1.1 (the "License"); you may not use this file except in compliance
// // with the License. You may obtain a copy of the License at
// //   
// // http://www.mozilla.org/MPL/
// //   
// // Software distributed under the License is distributed on an "AS IS"
// // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// // License for the specific language governing rights and limitations under 
// // the License.
// //   
// // The Initial Developer of the Original Code is Robert Smyth.
// // Portions created by Robert Smyth are Copyright (C) 2008-2013.
// //   
// // All Rights Reserved.

#endregion

using NoeticTools.NWorkflow.Exceptions;


namespace NoeticTools.NWorkflow.ProgressMonitoring
{
    internal class RootProgressScope : IProgressScope
    {
        private readonly NullProgressScope _nullScope;
        private IProgressScope _childScope;

        public RootProgressScope()
        {
            _nullScope = new NullProgressScope();
            _childScope = _nullScope;
        }

        public void Dispose()
        {
        }

        public IProgressScope Parent { get; private set; }
        public double TotalCompleted { get { return _childScope.TotalCompleted; } }

        public void Enter(IProgressScope childScope)
        {
            if (!ReferenceEquals(_childScope, _nullScope))
            {
                throw new WorkflowProgressConfigurationException("Progress flow may only be defined once.");
            }
            _childScope = childScope;
        }

        public void AddStep(IStepRules stepRules)
        {
            _childScope.AddStep(stepRules);
        }

        public double UpdateWeights(double startingWeight)
        {
            return _childScope.UpdateWeights(startingWeight);
        }

        public string Name
        {
            get { return "_Root"; }
        }
    }
}